package com.SmartCare.service.impl.message;

import com.SmartCare.common.Result;
import com.SmartCare.mapper.message.MsgBindElderMapper;
import com.SmartCare.mapper.role.ElderlyInformationMapper;
import com.SmartCare.mapper.role.EmployeeElderMapper;
import com.SmartCare.mapper.role.UserMapper;
import com.SmartCare.mapper.role.UserRelationshipMapper;
import com.SmartCare.pojo.message.MsgBindCareHome;
import com.SmartCare.pojo.message.MsgBindElder;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.pojo.role.EmployeeElder;
import com.SmartCare.pojo.role.User;
import com.SmartCare.pojo.role.UserRelationship;
import com.SmartCare.service.message.MsgBindElderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 绑定老人消息表 服务实现类
 */
@Service
public class MsgBindElderServiceImpl extends ServiceImpl<MsgBindElderMapper, MsgBindElder> implements MsgBindElderService {

    @Resource
    private UserRelationshipMapper userRelationshipMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ElderlyInformationMapper elderlyInformationMapper;

    @Resource
    private EmployeeElderMapper employeeElderMapper;

    @Override
    public Result query(String name) {
        QueryWrapper<MsgBindElder> qw = new QueryWrapper<>();
        qw.eq("care_home_name", name);
        qw.orderByDesc("create_time");
        return Result.success(list(qw));
    }

    @Override
    public Result updateMsg(MsgBindElder msg) {
        //更新消息状态
        updateById(msg);
        //绑定老人
        if (msg.getStatus() == 1) {
            //根据绑定用户的角色进行不同的绑定
            User user = userMapper.selectById(msg.getUserId());
            if (user.getRole() == 1) {
                employeeElderMapper.insert(EmployeeElder.builder()
                        .elderlyId(msg.getElderId())
                        .employeeId(msg.getUserId())
                        .build());
            } else if (user.getRole() == -1) {
                userRelationshipMapper.insert(UserRelationship.builder()
                        .elderlyId(msg.getElderId())
                        .familyMemberId(msg.getUserId())
                        .status(1)
                        .build());
            } else {
                return Result.error("该用户不是可绑定老人的角色");
            }
        }
        return Result.success();
    }

    @Override
    public Result addNewMsg(String careHomeName, Long userId, Long elderId) {

        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        ElderlyInformation elder = elderlyInformationMapper.selectById(elderId);
        if (elder == null) {
            return Result.error("老人不存在");
        }

        MsgBindElder msg = MsgBindElder.builder()
                .userId(userId)
                .careHomeName(careHomeName)
                .elderId(elderId)
                .build();
        save(msg);
        return Result.success();
    }
}
